import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

class StateManagermentDemo extends StatelessWidget {
  const StateManagermentDemo({Key key}) : super(key: key);
  final int count = 0;

  @override
  Widget build(BuildContext context) {
    return StateLessDemo();
  }
}

class StateLessDemo extends StatefulWidget {
  StateLessDemo({Key key}) : super(key: key);

  _StateLessDemoState createState() => _StateLessDemoState();
}

class _StateLessDemoState extends State<StateLessDemo> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: ScopedModel(
        model: CounterModel(),
        child: Scaffold(
          appBar: AppBar(
            title: Text('ScopModel'),
          ),
          body: CounterDemo(),
          floatingActionButton: ScopedModelDescendant<CounterModel>(
            builder: (context, _, model) => FloatingActionButton(
                  child: Icon(Icons.add),
                  onPressed: model.increaseCount,
                ),
          ),
        ),
      ),
    );
  }
}

class StateFullDemo extends StatefulWidget {
  @override
  _StateFullDemoState createState() => _StateFullDemoState();
}

class _StateFullDemoState extends State<StateFullDemo> {
  int count = 0;
  void increaseCount() {
    setState(() {
      count--;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      child: CounterProvider(
        count: count,
        increaseCount: increaseCount,
        child: Scaffold(
          appBar: AppBar(
            title: Text('TextManager'),
            elevation: 0.0,
          ),
          body: CounterDemo(),
          floatingActionButton: FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: () {
              setState(() {
                count++;
              });
            },
          ),
        ),
      ),
    );
  }
}

class CounterDemo extends StatelessWidget {
  const CounterDemo({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // final int count = CounterProvider.of(context).count;
    // final VoidCallback increaseCount =
    //     CounterProvider.of(context).increaseCount;
    return Container(
      child: Center(
          child: ScopedModelDescendant<CounterModel>(
        builder: (context, _, model) => ActionChip(
              label: Text('${model.count}'),
              onPressed: model.increaseCount,
            ),
      )),
    );
  }
}

class CounterProvider extends InheritedWidget {
  final Widget child;
  final int count;
  final VoidCallback increaseCount;
  CounterProvider({Key key, this.child, this.count, this.increaseCount})
      : super(key: key, child: child);

  static CounterProvider of(BuildContext context) {
    return (context.inheritFromWidgetOfExactType(CounterProvider)
        as CounterProvider);
  }

  @override
  bool updateShouldNotify(CounterProvider oldWidget) {
    return true;
  }
}

class CounterModel extends Model {
  int _count = 0;
  int get count => _count;

  void increaseCount() {
    _count--;
    notifyListeners();
  }
}
